<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<head>
<c:if test="${fn:contains(sessionInfo.resourceList, '/registerTreatment/edit')}">
	<script type="text/javascript">
		$.canEdit = true;
	</script>
</c:if>

<c:if test="${fn:contains(sessionInfo.resourceList, '/registerTreatment/charge')}">
	<script type="text/javascript">
		$.canCharge = true;
	</script>
</c:if>
<script>
	var registerTreatments = [];
	var registerId = "${registerId}";
	
	$('#treatmentItem').combogrid({    
	    url:'${ctx}/treatment/list',    
	    idField:'id',    
	    textField:'treatmentItem',
	    columns:[[
	        {field:'id',title:'治疗项目id',width:150},
	        {field:'treatmentItem',title:'治疗项目',width:150},
	        {field:'price',title:'价格',width:100}
	    ]]
	}); 
	
</script>

<c:forEach items="${registerTreatments}" var="registerTreatment">
	<script type="text/javascript">
		var registerTreatment = {
			'id':"${registerTreatment.id}",
			'treatmentItem': "${registerTreatment.treatmentItem}",
			'treatmentTime': "${registerTreatment.treatmentTime}",
			'chargeTime':"${registerTreatment.chargeTime}",
			'result':"${registerTreatment.result}",
		};

		registerTreatments.push(registerTreatment);
	</script>
</c:forEach>

<script type="text/javascript">
	
	$(function() {
		dataGrid = $('#dataGrid').datagrid({
			striped : true,
			rownumbers : true,
			singleSelect : true,
			idField : 'id',
			sortName : 'id',
			sortOrder : 'asc',
			columns :[[{
				width : '100',
				title : '治疗项目',
				field : 'treatmentItem'
			},{
				width : '100',
				title : 'id',
				field : 'id',
				hidden: true
			}, {
				width : '150',
				title : '治疗时间',
				field : 'treatmentTime',
			},{
				width : '150',
				title : '缴费时间',
				field : 'chargeTime'
			}, {
				width : '150',
				title : '结果',
				field : 'result'
			},{
				field : 'action',
				title : '操作',
				width : 200,
				formatter : function(value, row, index) {
					var str = '';
					if($.canEdit) {
						str += '&nbsp;&nbsp;';
						str  += "<a href=\"javascript:void(0)\" onclick=\"editResult(\'{1}\',\'{2}\');\" >编辑</a>";
					}
					if(row.chargeTime == '' && $.canCharge) {
						str += '&nbsp;&nbsp;';
						str += '<a href="javascript:void(0)" onclick="charge(\'{0}\');" >缴费</a>';
					}
					str = $.formatString(str, row.id,row.id,row.result);
					return str;
				}
			} ] ],
			data: registerTreatments,
			toolbar : '#toolbar'
		});
	});
	
	function charge(registerTreatmentId) {
		$.ajax({
			type: "POST",
			async: false,
			data:  {
				'registerTreatmentId':registerTreatmentId
			},
	        url: '${ctx}/registerTreatment/charge',
	        dataType: "json",
	        success:function(result) {
	        	if(result.status.errorCode == 0) {
	        		$.messager.alert('成功', result.status.errorMsg, 'info',function(r){
	        			parent.$.modalDialog.handler.dialog('refresh','${ctx}/registerTreatment/detailPage?registerId=' + registerId);
	        		});
	        	} else {
	        		$.messager.alert('错误', result.status.errorMsg, 'error');
	        	}
	        },
	        error:function(result) {
	        }
   	 	});
	}
	
	function editResult(registerTreatmentId,result) {
		var dialogHandler;
		parent.$.dialogHandler = dialogHandler = $('<div/>').dialog({
			title:'治疗结果',
			width:700,
			height:400,
			modal:true,
			href:'${ctx}/registerTreatment/editTreatmentPage?registerTreatmentId='+registerTreatmentId + "&result=" + result,
			onClose:function(){
				parent.$.modalDialog.handler.dialog('refresh','${ctx}/registerTreatment/detailPage?registerId=' + registerId);
				parent.$.dialogHandler = undefined;
				dialogHandler.dialog('destroy');
			},
			buttons : [ {
				text : '添加',
				handler : function() {
					debugger;
	        		var f = dialogHandler.find("#registerTreatmentEditForm");
	        		f.submit();
				}
			} ]
		});	
	}
	
	function addTreatment() {
		var treatmentId = $('#treatmentItem').combogrid('getValue');
		if(treatmentId == '') {
			$.messager.alert('错误', '未选择治疗项目', 'error');
		} else {
			var registerId = "${registerId}";
			
			$.ajax({
				type: "POST",
				async: false,
				data:  {
					'treatmentId':treatmentId,
					'registerId':registerId
				},
		        url: '${ctx}/registerTreatment/addTreatment',
		        dataType: "json",
		        success:function(result) {
		        	if(result.status.errorCode == 0) {
		        		$.messager.alert('成功', result.status.errorMsg, 'info',function(r){
		        			parent.$.modalDialog.handler.dialog('refresh','${ctx}/registerTreatment/detailPage?registerId=' + registerId);
		        		});
		        	} else {
		        		$.messager.alert('错误', result.status.errorMsg, 'error');
		        	}
		        },
		        error:function(result) {
		        }
	   	 	});
		}
	}
</script>
</head>

<div class="easyui-layout" data-options="fit:true,border:false">
	
	<div data-options="region:'center',fit:true,border:false">
		<table id="dataGrid" data-options="fit:true,border:false"></table>
	</div>
	
	<div id="toolbar" style="display: none;">
			<a onclick="addTreatment();" href="javascript:void(0);"
				class="easyui-linkbutton"
				data-options="plain:true,iconCls:'icon_add'">增加</a>
			<input id="treatmentItem" style="width: 400px; "></input>
	</div>
</div>
